package com.hr.controller;

import com.hr.dto.Page;
import com.hr.dto.Result;
import com.hr.entity.User;
import com.hr.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "用户管理")
@CrossOrigin
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    public Result addUser(@ApiParam(value = "用户对象", required = true) @RequestBody User user){
        userService.addUser(user);
        return Result.ok();
    }

    @ApiOperation("删除用户")
    @GetMapping("/deleteUser")
    public Result deleteUser(@ApiParam(value = "用户id", required = true) @RequestParam("userId") Integer userId){
        userService.deleteUser(userId);
        return Result.ok();
    }

    @ApiOperation("更新用户")
    @PostMapping("/updateUser")
    public Result updateUser(@ApiParam(value = "用户对象", required = true) @RequestBody User user){
        userService.updateUser(user);
        return Result.ok();
    }

    @ApiOperation("获取用户")
    @GetMapping("/getUser")
    public Result getUser(@ApiParam("用户名") @RequestParam("userName") String userName, @ApiParam("邮箱") String email, @ApiParam("状态") Integer status){
        User user = new User();
        user.setUserName(userName);
        user.setEmail(email);
        user.setStatus(status);
        User u = userService.getUser(user);
        return Result.ok(u);
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/getUserList")
    public Result getUserList(@ApiParam(value = "当前页码", required = true) @RequestParam("pageNo") Integer pageNo,
                              @ApiParam(value = "显示条数", required = true) @RequestParam("pageSize") Integer pageSize){
        Page page = userService.getUserPage(pageNo, pageSize);
        return Result.ok(page);
    }

}
